<?php

namespace app\admin\model;

use app\lib\Qiniu;

class Member extends Base
{

    // 数据表名称
    //protected $table;

    // 当前模型名称 不带前缀
    protected $name = 'member';

    // 数据表主键 复合主键使用数组定义 不设置则自动获取
    protected $pk = 'id';


    public static function _after_delete($id){
        if(is_array($id)){
            $list = Image::where('related_table', 'member_level')->whereIn('related_id', $id)->select();
        }else{
            $list = Image::where('related_table', 'member_level')->where('related_id', $id)->select();
        }

        $Qiniu = new Qiniu();
        foreach ((array)$list as $item) {
            $key = substr(strrchr($item['img_url'], '/'), 1);
            // 删除七牛图片
            $res = $Qiniu->delImg($key);

            Image::where('id', $item['id'])->delete();
        }
    }


    // 昵称字符转换
    public function getnickNameAttr($data){
        return urldecode($data);
    }

    public static function getByUid($uid){
        $user = self::where('id','=',$uid)->find();
        return $user;
    }
    public static function getByOpenId($openid){
        $openid  = self::where('open_id','=',$openid)->value('open_id');

        return $openid;
    }
    public static function checkUser($uid){
        if(!empty($uid)){
            $res = self::where('id',$uid)->count();
            if($res > 0){
                return true;
            }
            return false;
        }
        return false;
    }
}